package com.yzh.design.demo.util;

import java.util.*;

public class TopicRandomUtil {
    /**
     * 思路：将options的key放入集合中，打乱顺序，然后源key的value和乱序key去匹配放入新的map中返回。
     * @param options  key是选项：A、B、C、D  value是题目
     * @param key     正确答案
     * @return
     */
    public static Topic random(Map<String, String> options, String key) {
        Set<String> originMapKey = options.keySet();
        //乱序
        List<String> disOrderMapKey = new ArrayList<>(originMapKey);
        Collections.shuffle(disOrderMapKey);
        //存储新结果集
        Map<String, String> optionNew = new HashMap<>();
        String keyNew = "";
        int index = 0;
        for (String s : originMapKey) {
            //将正确答案替换为乱序后的
            if (key.equals(s)) {
                keyNew = disOrderMapKey.get(index);
            }
            //将乱序后的key和源value存入
            optionNew.put(disOrderMapKey.get(index++), options.get(s));
        }
        return new Topic(optionNew, keyNew);
    }
}
